#include "modbuswig.h"
#include "ui_modbuswig.h"

ModbusWig::ModbusWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModbusWig)
{
    ui->setupUi(this);
    QObject::connect(this, SIGNAL(dataUpSignal(QByteArray)),
                     this, SLOT(dataUpSlot(QByteArray)));
    //最大刻度间的间距
    ui->dial_2->setPageStep(100);
    //刻度可见
    ui->dial_2->setNotchesVisible(true);
    //设置两个凹槽间的目标像素数
    ui->dial_2->setNotchTarget(10.00);
    //设置范围
    ui->dial_2->setRange(800,1400);
    /* 实例化对象和设置显示位置与大小 */
    //    ui->label_6 = new QLabel(this);
    //    ui->label_6->setGeometry(370, 300, 200, 50);

    /* 初始化为0km/h */
    ui->label_6->setText("800Pa");

    /* 信号槽连接 */
    connect(ui->dial_2, SIGNAL(valueChanged(int)),
            this, SLOT(dialValueChanged(int)));

    //最大刻度间的间距
    ui->dial_3->setPageStep(1);
    //刻度可见
    ui->dial_3->setNotchesVisible(true);
    //设置两个凹槽间的目标像素数
    ui->dial_3->setNotchTarget(1.00);
    //设置范围
    ui->dial_3->setRange(15,25);


    /* 初始化为0km/h */
    ui->label_7->setText("15 %");

    /* 信号槽连接 */
    connect(ui->dial_3, SIGNAL(valueChanged(int)),
            this, SLOT(dialyValueChanged(int)));
        connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(pushButton_3_shuaxin()));


//    QPainter *painter(ui->dial_3);
////    painter.draws

//    drawScaleNum(painter);
//    ui->pushButton->setStyleSheet("background-color: rgb(71, 126, 255);");
    ui->dial->setEnabled(false);
    ui->dial_2->setEnabled(false);
    ui->dial_3->setEnabled(false);

    timer = new QTimer(this);

    // 提前刷新显式控件
    timeoutSlot();

    timer->setInterval(500);   // 设置时间间隔1秒钟
    timer->setSingleShot(false);    // 为周期循环

    // 连接信号槽要在启动定时器之前连接
    connect(timer,SIGNAL(timeout()),this,
        SLOT(timeoutSlot()));

    // 启动定时器
    timer->start();
}

ModbusWig::~ModbusWig()
{
    delete ui;
}

//void ModbusWig::drawScaleNum(QPainter *painter)
//{

////    paintersave();
//    painter->save();

//    qDebug()<< "drawText";
////    int r = (int)(radius*0.6);
//      int r = (int)(5*0.6);

//    painter->setFont(QFont("Arial", 10));
//    painter->setPen(QPen(QColor(255,255,255)));
//    QFontMetricsF fm = QFontMetricsF(painter->font());

//        int Angle = 45;
//    int gap = (360-Angle*2) / 10;
////    int gap = (360-45*2) / 10;
//    for(int i=0; i<=10; i+=1)
//    {
//        int angle = 90+Angle+gap*i;  //角度,10格子画一个刻度值

//        float angleArc =( angle % 360) * 3.14 / 180; //转换为弧度
//        int x = (r)*cos(angleArc);
//        int y = (r)*sin(angleArc);

//        QString value = QString::number(i*100);
//        int w = (int)fm.width(value);
//        int h = (int)fm.height();
//        x = x - w/2;
//        y = y + h/4;

//        qDebug()<< "x:"<<x<<" y:"<<y;
//        //painter->drawPoint(QPointF(x, y));
//        painter->drawText(QPointF(x, y),value);
//    }
//    painter->restore();

//}
//接收数据并显示
void ModbusWig::dataUpSlot(QByteArray text)
{
    QString value(text);
    QJsonParseError parseJsonErr;
    QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(),&parseJsonErr);
    QJsonObject jsonObject = document.object();
    qDebug()<<"type="<<jsonObject["type"].toInt();

    if(jsonObject.contains(QStringLiteral("result")))
    {
        QJsonValue jsonValueList = jsonObject.value(QStringLiteral("result"));
        QJsonObject item = jsonValueList.toObject();
        qDebug()<<"result="<<item["result"].toInt();
    }

    if(jsonObject.contains(QStringLiteral("data")))
    {
        QJsonValue arrayValue = jsonObject.value(QStringLiteral("data"));
        if(arrayValue.isArray())
        {
            QJsonArray array = arrayValue.toArray();
            for(int i=0;i<array.size();i++)
            {
                QJsonValue iconArray = array.at(i);
                QJsonObject icon = iconArray.toObject();
                int key = icon["key"].toInt();
                QString val = icon["val"].toString();
                qDebug()<<"key="<<key<<"val="<<val;

                switch (key) {

                case 101:
                    if(val == "0")
                    {
//                        ui->pushButton->setStyleSheet("background-color: rgb(71, 126, 255);");
                        ui->pushButton->setChecked(false);

                    }
                    else
                    {
//                        ui->pushButton->setStyleSheet("background-color: rgb(255, 174, 60);");
                        ui->pushButton->setChecked(true);
                    }
                    break;
                    //                  case 102:
                    //                      ui->lineEdit_3->setText(val);
                    //                      break;
                case 103:
                    ui->lineEdit_yang->setText(val);
                    ui->dial_3->setValue(val.toInt());
                    break;
                case 104:
                     ui->lineEdit_ya->setText(val);

                    ui->dial_2->setValue(val.toInt());
                    break;
                case 105:
                    ui->lineEdit_hai->setText(val);
                    break;
                case 106:
                    ui->lineEdit_people->setText(val);
                    break;

                default:
                    break;
                }

            }
        }
    }
}

void ModbusWig::on_pushButton_clicked()
{
    if(kongtiao==1)
    {
//        ui->pushButton->setStyleSheet("background-color: rgb(71, 126, 255);");
        QMQTT::Message aaa;
        aaa.setTopic("down");
        aaa.setPayload("{\"type\": 2, \"data\": { \"key\": \"101\",\"val\": \"0\"}}");
        GlobalVal::mqtt->publish(aaa);
        ui->dial->setEnabled(false);
        kongtiao=0;

    }
    else
    {
//        ui->pushButton->setStyleSheet("background-color: rgb(255, 174, 60);");
        QMQTT::Message aaa;
        aaa.setTopic("down");
        aaa.setPayload("{\"type\": 2, \"data\": { \"key\": \"101\",\"val\": \"1\"}}");
        GlobalVal::mqtt->publish(aaa);
        ui->dial->setEnabled(true);
        kongtiao=1;
    }
}

//void ModbusWig::on_pushButton_2_clicked()
//{
//    QMQTT::Message aaa;
//    aaa.setTopic("down");
//    QString ss=ui->spinBox->text();
//    QString strtemp;
//    strtemp = QString("{\"type\": 2, \"data\": { \"key\": \"102\",\"val\": %1}}").arg(ss);
//    aaa.setPayload(strtemp.toUtf8());
//    GlobalVal::mqtt->publish(aaa);

//}




//温度控制
void ModbusWig::on_dial_actionTriggered(int action)
{
    QMQTT::Message aaa;
    aaa.setTopic("down");
    int s=ui->dial->value();
    QString ss=QString::number(s);
    QString strtemp;
    strtemp = QString("{\"type\":2,\"data\":{\"key\":\"102\",\"val\":\"%1\"}} ").arg(ss);
    aaa.setPayload(strtemp.toUtf8());
    qDebug() << aaa.payload();
    GlobalVal::mqtt->publish(aaa);

    ui->label->setText(ss.append("℃"));
}

void ModbusWig::dialValueChanged(int value)
{
        QString val=QString::number(value);
    ui->label_6->setText(QString::number(value).append("Pa"));
//        QPainter *painter(ui->dial_2);
//    //    painter.draws

//        drawScaleNum(painter);

}

void ModbusWig::dialyValueChanged(int value)
{
    QString val=QString::number(value);
    ui->label_7->setText(QString::number(value).append(" %"));
}

void ModbusWig::pushButton_3_shuaxin()
{
    QMQTT::Message aaa;
    aaa.setTopic("down");
    aaa.setPayload("{\"type\": 1,\"limit\": \"all\",\"keys\": [301, 302, 303, 304 , 305, 306, 307, 308, 101, 102, 103, 104, 105, 106]}");
    GlobalVal::mqtt->publish(aaa);
}

void ModbusWig::timeoutSlot()
{
    // 获取当前系统时间，转换为：时:分:秒
        QString str = QDateTime::currentDateTime().toString("hh:mm:ss");
        ui->lcdNumber->display(str);
}

